package org.aeon.client.view.components.listColumn;

import org.aeon.client.view.components.button.ActiveContactButton;
import org.aeon.client.view.components.button.ExpansionButton;
import org.aeon.client.view.components.sessionPanel.SessionPanel;
import org.aeon.client.view.components.textField.SearchField;
import org.aeon.entity.dto.GroupInfoDto;
import org.aeon.entity.dto.UserInfoDto;
import org.aeon.utils.TimeUtil;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.*;

/**
 * @author: MissingAeon
 * @date: 2024/2/20 13:37
 * @description:
 */
public class MessageListPanel extends JPanel {

    private final ColumnScrollPanel scrollPane;
    private final SessionPanel sessionPanel;
    private final List<ActiveContactButton> buttons;

    public MessageListPanel(SessionPanel sessionPanel) {
        this.sessionPanel = sessionPanel;
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        this.add(Box.createVerticalStrut(11));
        final Box topBox = Box.createHorizontalBox();
        topBox.add(Box.createHorizontalGlue());
        // 创建搜索输入框
        final SearchField searchField = new SearchField(new Dimension(240, 30));
        searchField.setPlaceholder("本地查询好友和群聊...");
        topBox.add(searchField);
        topBox.add(Box.createHorizontalStrut(10));
        topBox.add(ExpansionButton.createExpansionButton());
        topBox.add(Box.createHorizontalGlue());
        this.add(topBox);

        this.add(Box.createVerticalStrut(10));


        this.buttons = new ArrayList<>();
        scrollPane = new ColumnScrollPanel(new Dimension(300, 687));
        scrollPane.setBorder(null);
        // 将 JScrollPane 添加到 JFrame 中
        this.add(scrollPane);
    }

    /**
     * 添加用户活动联系人按钮
     *
     * @param user
     * @param date
     */
    public void addItem(UserInfoDto user, Date date) {
        if (match(user).isEmpty()) {
            final ActiveContactButton button = new ActiveContactButton(user, TimeUtil.format(date), sessionPanel);
            buttons.add(button);
            scrollPane.addItem(button);
        }
    }

    /**
     * 添加群聊活动联系人按钮
     *
     * @param group
     * @param date
     */
    public void addItem(GroupInfoDto group, Date date) {
        if (match(group).isEmpty()) {
            final ActiveContactButton button = new ActiveContactButton(group, TimeUtil.format(date), sessionPanel);
            buttons.add(button);
            scrollPane.addItem(button);
        }
    }

    /**
     * 移除群聊活动联系人按钮
     *
     * @param group
     */
    public void removeItem(GroupInfoDto group) {
        match(group).ifPresent(scrollPane::removeItem);
    }


    /**
     * 移除用户活动联系人按钮
     *
     * @param user
     */

    public void removeItem(UserInfoDto user) {
        match(user).ifPresent(scrollPane::removeItem);
    }

    private Optional<ActiveContactButton> match(UserInfoDto user) {
        return buttons.stream()
                .filter(button -> {
                    if (button.getContent() instanceof UserInfoDto userInfoDto) {
                        return Objects.equals(user.getId(), userInfoDto.getId());
                    }
                    return false;
                }).findAny();
    }

    private Optional<ActiveContactButton> match(GroupInfoDto group) {
        return buttons.stream()
                .filter(button -> {
                    if (button.getContent() instanceof GroupInfoDto groupInfoDto) {
                        return Objects.equals(group.getId(), groupInfoDto.getId());
                    }
                    return false;
                }).findAny();
    }
}
